﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Artem.Google.Net;
using Artem.Web.UI.Controls;

namespace GloberoSport.Interfaz
{
    public partial class MostrarRuta : System.Web.UI.Page
    {
        int idRuta = 0;
        string sUsuario;
        Negocio.Track tr;
        protected void Page_Load(object sender, EventArgs e)
        {
            obtenerUsuario();
            cargarGMAP();
            cargarChart();
            cargarRepeater();
        }

        private void obtenerId()
        {
        }

        private void obtenerUsuario()
        {
            sUsuario = User.Identity.Name;
        }

        private void cargarGMAP()
        {
            List<Negocio.Track> lista = new List<Negocio.Track>();
            lista = Negocio.Track.obtenerTrackUltimo(sUsuario);
            if (lista.Count > 0)
            {
                tr = lista[lista.Count - 1];
                int iPuntos = tr.Lista.Count;
                int i = 0;

                lblFINI.Text = tr.FechaIni.ToString("dd/MM/yyyy hh:mm");
                lblDIST.Text = tr.Distancia.ToString() + " km.";
                lblAMAX.Text = (tr.AltMax - 54).ToString() + " m.";
                lblATOT.Text = tr.AltTotal.ToString() + " m.";
                int iHoras = tr.TsTotal / 3600;
                int iMinutos = (tr.TsTotal - (iHoras * 3600)) / 60;
                int iSegundos = (tr.TsTotal - (iHoras * 3600) - (iMinutos * 60));
                lblTSTOT.Text = iHoras + "h " + iMinutos + "' " + iSegundos + "\" ";



                gMap1.Polylines.Clear();
                GooglePolyline gpl = new GooglePolyline();
                gpl.Color = System.Drawing.Color.Red;
                if (iPuntos > 0)
                {

                    gMap1.Latitude = (double)tr.Lista[0].Lat;
                    gMap1.Longitude = (double)tr.Lista[0].Lon;
                    gMap1.Zoom = 12;
                    int it = tr.Lista.Count / 2 + 2;

                    string rutaFinal = "from: " + tr.Lista[0].Lat.ToString().Replace(",", ".") + ", " + tr.Lista[0].Lon.ToString().Replace(",", ".") + " to: " + tr.Lista[it].Lat.ToString().Replace(",", ".") + ", " + tr.Lista[it].Lon.ToString().Replace(",", ".");

                    GoogleDirection gd = new GoogleDirection(rutaFinal);
                    gMap1.Directions.Add(gd);
                }
            }
        }
        private void cargarRepeater()
        {
            //rep1.DataSource = tr.Lista;
            //rep1.DataBind();
        }
        private void cargarChart()
        {
            Chart1.Series.Clear();
            Chart1.Palette = System.Web.UI.DataVisualization.Charting.ChartColorPalette.None;
            Chart1.PaletteCustomColors = new System.Drawing.Color[] { System.Drawing.Color.Blue, System.Drawing.Color.Red, System.Drawing.Color.Green, System.Drawing.Color.Orange };

            Chart1.Series.Add("PerfilEtapa");
            //Chart1.Series["IntEntrada"].Points.DataBindXY(, yVal);
            Chart1.Series["PerfilEtapa"].XValueMember = "DistanciaAcumulada";
            Chart1.Series["PerfilEtapa"].YValueMembers = "AlturaTratada";
            //Chart1.Series["PerfilEtapa"].XValueMember = "AlturaTratada";
            //Chart1.Series["PerfilEtapa"].YValueMembers = "DistanciaAcumulada";
            Chart1.Series["PerfilEtapa"].AxisLabel = "distancia";
            //Chart1.Series["PerfilEtapa"].Legend = "Perfil Etapa";
            //Chart1.Series["PerfilEtapa"].L
            //Chart1.Series["PerfilEtapa"].LegendText = "#VALX #PERCENT{P0}"; 
            //Chart1.Legends.Add("Leg1");
            //Chart1.Legends["Leg1"].Position.Auto = true;

            //SET THE CHART TYPE TO BE PIE
            Chart1.Series["PerfilEtapa"].ChartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.SplineArea;

            Chart1.ChartAreas.Clear();
            Chart1.ChartAreas.Add("ChartArea");

            Chart1.DataSource = tr.Lista;
            Chart1.DataBind();
        }
    }
}